﻿using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UsedBookTradeSystem.DBContext;
using UsedBookTradeSystem.Models.Dtos.InputDtos;
using UsedBookTradeSystem.Models.Entity;
using UsedBookTradeSystem.Models.Enum;

namespace UsedBookTradeSystem.Controllers.BackendControllers
{
    public class UserController : Controller
    {

        private readonly SystemDbContext _ctx;

        public UserController(SystemDbContext ctx)
        {
            _ctx = ctx;
        }
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Index()
        {
            var UserList = await _ctx.Users.Where(x => x.Role == RoleEnum.User).ToListAsync();
            ViewBag.UserList = UserList;
            return View();
        }
        [Authorize(Roles = "Admin")]
        public IActionResult Add()
        {
            return View();
        }
        [Authorize(Roles = "Admin")]
        public IActionResult Edit(int id)
        {
            var user = _ctx.Users.FirstOrDefault(x => x.Id == id);
            ViewBag.User = user;
            return View();
        }
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> ChangeStatus(int id)
        {
            var user = await _ctx.Users.FirstAsync(x => x.Id == id);
            user.IsDisabled = !user.IsDisabled;
            _ctx.Users.Update(user);
            await _ctx.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> AddUser(UpdateUserInput userInput)
        {
            if (string.IsNullOrWhiteSpace(userInput.UserName) ||
                string.IsNullOrWhiteSpace(userInput.PassWord) ||
                string.IsNullOrWhiteSpace(userInput.ConfirmPassword) ||
                string.IsNullOrWhiteSpace(userInput.Tel) ||
                userInput.PassWord != userInput.ConfirmPassword ||
                userInput.Tel.Length !=11)
            {
                TempData["OperateResult"] = "输入不合法！";
                return RedirectToAction(nameof(Index));
            }
            if (!UInt64.TryParse(userInput.Tel, out UInt64 n))
            {
                TempData["OperateResult"] = "请输入正确的手机号码！";
                return RedirectToAction(nameof(Index));
            }
            var CheckUser = await _ctx.Users.FirstOrDefaultAsync(x => x.UserName == userInput.UserName);
            if (CheckUser is not null)
            {
                TempData["OperateResult"] = "用户名已存在！";
                return RedirectToAction(nameof(Index));
            }
            var user = userInput.Adapt<User>();
            user.Cart = new()
            {
                User = user
            };
            await _ctx.Users.AddAsync(user);
            await _ctx.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> UpdateUser(UpdateUserInput userInput) 
        {
            if (string.IsNullOrWhiteSpace(userInput.UserName) ||
                string.IsNullOrWhiteSpace(userInput.Tel) ||
                userInput.Tel.Length != 11)
            {
                TempData["OperateResult"] = "输入不合法！";
                return RedirectToAction(nameof(Index));
            }
            if (!UInt64.TryParse(userInput.Tel, out UInt64 n))
            {
                TempData["OperateResult"] = "请输入正确的手机号码！";
                return RedirectToAction(nameof(Index));
            }
            var user = await _ctx.Users.FirstOrDefaultAsync(x => x.Id == userInput.Id);
            if (user == null) 
            {
                TempData["OperateResult"] = "用户不存在！";
                return RedirectToAction(nameof(Index));
            }
            if (!string.IsNullOrWhiteSpace(userInput.PassWord)&&!string.IsNullOrWhiteSpace(userInput.ConfirmPassword)) 
            {
                if (userInput.PassWord == userInput.ConfirmPassword) 
                {
                    user.PassWord = userInput.PassWord;
                }
                else 
                {
                    TempData["OperateResult"] = "两次密码不一致！";
                    return RedirectToAction(nameof(Index));
                }
            }
            user.UserName = userInput.UserName;
            user.Tel = userInput.Tel;
            user.Gender = userInput.Gender;
            user.Address = userInput.Address;
            _ctx.Users.Update(user);
            await _ctx.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
    }
}
